package com.xls.boxlibrary.duihuanjibi;

import android.app.Activity;
import android.content.Context;


import com.xls.boxlibrary.entity.AppsRulesEntity;
import com.xls.boxlibrary.entity.DuiHuanJiBiInfoEntity;
import com.xls.boxlibrary.entity.JiFenDuiHuanJiLuEntity;
import com.xls.boxlibrary.entity.JiFenMingXiEntity;
import com.xls.boxlibrary.entity.JiFenTiXianBiliEntity;
import com.xls.boxlibrary.entity.PageEntity;
import com.xls.boxlibrary.entity.TiXianJiLuEntity;
import com.xls.boxlibrary.entity.UserEntity;
import com.xls.boxlibrary.network.RequestCallBack;
import com.xls.boxlibrary.network.RequestCallBackTwo;
import com.xls.boxlibrary.network.RequestManager;
import com.xls.boxlibrary.network.RetrofitBase;
import com.xls.boxlibrary.utils.AppUtils;
import com.xls.boxlibrary.utils.TUtils;
import com.xls.commonlibrary.app.MainApplication;
import com.xls.commonlibrary.http.box.SPUtils;
import com.xls.commonlibrary.util.AESUtil;
import com.xls.commonlibrary.util.MMKVUtil;
import com.xls.commonlibrary.util.SysDataUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.xls.commonlibrary.wxapi.ConstWx.KEY_WX_HEADIMGURL;
import static com.xls.commonlibrary.wxapi.ConstWx.KEY_WX_NICKNAME;
import static com.xls.commonlibrary.wxapi.ConstWx.KEY_WX_OPEN_ID;
import static com.xls.commonlibrary.wxapi.ConstWx.KEY_WX_UNION_ID;

public class JinBiNetWorkUtil {
    /**
    * 单例模式
    **/
    private static JinBiNetWorkUtil mJinBiNetWorkUtil = null;
        /**
         * 构造函数私有化
         **/
        private JinBiNetWorkUtil() {
        }
        /**
         * 公有的静态函数，对外暴露获取单例对象的接口
         **/
        public static JinBiNetWorkUtil getInstance() {
            if (mJinBiNetWorkUtil == null) {
                synchronized (JinBiNetWorkUtil.class) {
                    if (mJinBiNetWorkUtil == null) {
                        mJinBiNetWorkUtil = new JinBiNetWorkUtil();
                    }
                }
            }
            return mJinBiNetWorkUtil;
        }


    /**
     * 获取提现额度tab
     * @param appId
     * @param callBack
     */
    public void getAppRules(Context context,String appId,String packageName,RequestCallBack<ArrayList<AppsRulesEntity>> callBack) {
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getAppRules(appId,packageName),callBack);

    }

    public void getUserLevel(Context context,String appId , String packageName, RequestCallBack<UserEntity> callBack) {
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getUserLevel(appId,packageName),callBack);
    }

    public void getScoreExchangeRateRules(Context context,String appId,String packageName, RequestCallBack<ArrayList<JiFenTiXianBiliEntity>> callBack) {
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getScoreExchangeRateRules(appId,packageName),callBack);
    }

    /**
     * 获取游戏积分数据
     * @param appId
     * @param packageName
     * @param callBack
     */
    public void getAccountByAppId(Context context,String appId, String packageName, RequestCallBack<JiFenMingXiEntity> callBack) {
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getAccountByAppId(appId,packageName),callBack);
    }

    public void getScoreExchange(Context context, String score, String typeId, String appID, String packageName, RequestCallBackTwo<DuiHuanJiBiInfoEntity> callBack) {

        // 微信id
        String openId = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_OPEN_ID);
        String unionId = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_UNION_ID);

        // 微信头像etc   新增
        String headImageUrl = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_HEADIMGURL);
        String nickName = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_NICKNAME);

        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getScoreExchange(score,typeId,appID,packageName,unionId,openId,headImageUrl,nickName,"0"), callBack);
    }


    public void coinsExchange(Context context,String numStr,RequestCallBack<String> callBack) {

        // 微信id
        String openId = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_OPEN_ID);
        String unionId = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_UNION_ID);

        // 微信头像etc   新增
        String headImageUrl = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_HEADIMGURL);
        String nickName = MMKVUtil.getInstance().getKV().decodeString(KEY_WX_NICKNAME);
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                coinsExchange(numStr,unionId,openId,headImageUrl,nickName, AppUtils.getPackageName(MainApplication.getAppContext()),0+""),callBack);
    }


    public void getMyCoinsExchangeRecords(Context context,int pageSize, int pageNum, RequestCallBack<PageEntity<TiXianJiLuEntity>> callBack) {
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getMyCoinsExchangeRecords(pageSize, pageNum),callBack);

    }

    public void getMyScoreExchangeRecords(Context context,int pageSize, int pageNum,String appId, RequestCallBack<PageEntity<JiFenDuiHuanJiLuEntity>> callBack) {
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getMyScoreExchangeRecords(pageSize, pageNum,appId,1+""),callBack);
    }
    public void fkCheck(Activity activity,String source, Map<String, String> stringStringMap, RequestCallBack<String> stringRequestCallBack) {
        Map<String, String> data= new LinkedHashMap<>();
        stringStringMap.put("source",source);
        data.put("mdKey", AESUtil.httpRequestProcess(stringStringMap,source));
        data.put("apps", SysDataUtil.getInstance().getPkgList());
        RetrofitBase.getInstace(activity).setSubscribe(activity, RequestManager.getInstance(activity).
                fkCheck(data), new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(String requestData) {
                stringRequestCallBack.onRequestSuccess(requestData);
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {
                if (errno.equals("2")){
                    TUtils.showShort(activity,"手机太累了，该休息休息了");
                }else if (errno.equals("3")){
                    TUtils.showShort(activity,"提现被拒绝");
//                    SPUtils.saveLoginToken(activity,"");
//                    LoginUtil.getInstance().outLogin(activity);
                }else {
                    TUtils.showShort(activity,errmsg);
                }

            }
        });
    }

    public void getOauthConfig(Context context,RequestCallBack<HuiFuData> callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("appName",AppUtils.getPackageName(context));
        params.put("openid", MMKVUtil.getInstance().getKV().decodeString(KEY_WX_OPEN_ID));
//        params.put("account", MMKVUtil.getInstance().getKV().decodeString(KEY_WX_UNION_ID));
//        JSONObject data = new JSONObject(params);
//        String par = Demo.encode(Demo.comSecret,data.toString());//参数加密
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("comKey", BuildConfig.comKey);//comKey
//        map.put("timestamp", System.currentTimeMillis() / 1000);
//        map.put("data", par);
        RetrofitBase.getInstace(context).setSubscribe(context, RequestManager.getInstance(context).
                getOauthConfig(params),callBack);

    }

    public class HuiFuData implements Serializable {
        String is_auth;
        String hf_appid;
        String gh_id;
        String path;
        String query;
        String openId;

        public String getIs_auth() {
            return is_auth;
        }

        public void setIs_auth(String is_auth) {
            this.is_auth = is_auth;
        }

        public String getHf_appid() {
            return hf_appid;
        }

        public void setHf_appid(String hf_appid) {
            this.hf_appid = hf_appid;
        }

        public String getGh_id() {
            return gh_id;
        }

        public void setGh_id(String gh_id) {
            this.gh_id = gh_id;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getQuery() {
            return query;
        }

        public void setQuery(String query) {
            this.query = query;
        }

        public String getOpenId() {
            return openId;
        }

        public void setOpenId(String openId) {
            this.openId = openId;
        }
    }
}
